import 'package:ayj_flutter_chat_app/common/third/agora/engine/agora_engine_controller.dart';
import 'package:ayj_flutter_chat_app/pages/chat/voice/voice_controller.dart';
import 'package:ayj_flutter_chat_app/pages/chat/voice/widget/voice_back_widget.dart';
import 'package:ayj_flutter_chat_app/pages/chat/voice/widget/voice_buttons_widget.dart';
import 'package:ayj_flutter_chat_app/pages/chat/voice/widget/voice_header_widget.dart';
import 'package:ayj_flutter_chat_app/pages/chat/voice/widget/voice_shrink_widget.dart';
import 'package:ayj_flutter_chat_app/pages/chat/voice/widget/voice_time_widget.dart';
import 'package:flutter/material.dart';
import 'package:ayj_flutter_chat_app/common/configs/project_config.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../../common/utils/jh_permission_utils.dart';

class VoicePage extends GetWidget<VoiceController> {

  static const String sName = '/voice_page';

  static Future<void> open(BuildContext context, {
    dynamic arguments,
  }) async {
    bool status = await JhPermissionUtils.checkPermissions([Permission.microphone]);
    if (status) {
      Get.put(AgoraEngineController());
      JhNavUtils.pushNamed(context, VoicePage.sName,arguments: arguments);
    } else {
      JhPermissionUtils.JhDialogOpenAppSettings(context);
      return;
    }
  }

  final Map? arguments;
  const VoicePage({Key? key, this.arguments});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset:false,
        appBar: _buildAppBar(),
        body: _buildBody(context)
    );
  }

  PreferredSizeWidget? _buildAppBar(){
    return null;
  }

  Widget _buildBody(context){
    AgoraEngineController engineController = AgoraEngineController.to;
    double margin = engineController.buttonsW * .6;
    double bottomSize = engineController.buttonsW * 1.5;
    String url = 'https://randomuser.me/api/portraits/women/17.jpg';

    return Container(
        color: Colours.colorFDFFFE,
        child: Stack(
          children: [
            VoiceBackWidget(url: url),
            Positioned(
              left: margin,
              right: margin,
              bottom: bottomSize,
              height: engineController.buttonsW,
              child: Obx(()=> VoiceButtonsWidget(
                buttonW: engineController.buttonsW,
                type: engineController.voiceMeetingType.value,
                cType: ControllerType.meetingVoice,
                receiveOnTap: (){
                  engineController.receiveOnTap('1',1);
                },
                cancelOnTap: (){
                  engineController.cancelOnTap();
                },
                speakOnTap: engineController.speakOnTap,
                voiceOnTap: engineController.voiceOnTap,
              )),),
            Positioned(
              left: 0,
              right: 0,
              bottom: bottomSize * 2 + 30.h,
              child: Center(
                child: Obx(()=> VoiceTimeWidget(type: engineController.voiceMeetingType.value,
                  cType: ControllerType.meetingVoice,
                ),
                ),),
            ),
            Positioned(
                top: 32.h+JhScreenUtils.topSafeHeight,
                left: 32.w,
                child: VoiceShrinkWidget(cType: ControllerType.meetingVoice,
                    onTap:(v){}
                ),
            ),
            Positioned(
                top: JhScreenUtils.screenHeight * 0.3,
                left: 0,
                right: 0,
                child: Center(
                  child: VoiceHeaderWidget(
                    url: url,
                    name: '贾乃亮',
                    size: engineController.buttonsW*1.5,
                  ),
                ))
          ],
        )
    );
  }

}
